<?php
/**
 * @desc 订单管理
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class OrderController extends Controller
{
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin',
			'AjaxReq'
        );
    }

	//首页
	public function actionIndex()
	{
		$request = Yii::app()->request;
		$current = $request->getParam('pageNum');
		$numPerPage = $request->getParam('numPerPage');
		$goods_name = $request->getParam('goods_name');
		$pay_status = $request->getParam('pay_status');
		$travel_date = $request->getParam('travel_date');
		$order_date = $request->getParam('order_date');
		$page['current'] = empty($current)?1:$current;
		$page['numPerPage'] = empty($numPerPage)?Yii::app()->params['numPerPage']:$numPerPage;
		$page['goods_name'] = $goods_name;
		$page['pay_status'] = $pay_status;
		$page['travel_date'] = $travel_date;
		$page['order_date'] = $order_date;
		//数据查询
		$model = new Order;
		$list = $model->getOrderList($page);
		$count = array_pop($list);
		$page['total'] = $count[0];
		$this->render('index', array(
				'page' => $page,
				'list' => $list
			));
	}
	//添加私人订制
	/*public function actionAdd(){
		//获取参数
		$request = Yii::app()->request;
		$form = $request->getParam('dataForm');
		if(!empty($form)){
			$model = new PtravelForm('add');
			$model->attributes = $form;
			if(!$model->validate() || !$model->saveData()){
				$msg = array_values($model->getErrors());
				$this->output['status'] = 10000;
				$this->output['msg'] = $msg[0];
				$this->renderJson($this->output);
			}
			$this->output['code'] = 200;
			$this->output['navTabId'] = 'ptravel';
			$this->output['callbackType'] = 'closeCurrent';
			$this->renderJson($this->output);
		}
		$model = new Goods;
		$goodsList = $model->getGoodsValueList();
		$this->render("add", array(
				'goodsList'=>$goodsList
			));
	}*/
	//删除订单
	public function actionDelete(){
		//获取参数
		$request = Yii::app()->request;
		$ids = $request->getParam('ids');
		//删除操作
		$model = new Order;
		$result = $model->deleteOrder($ids);
		if(empty($result)){
			$this->output['status'] = 10003;
			$this->renderJson($this->output);
		}
		$this->output['code'] = 200;
		$this->output['navTabId'] = 'order';
		$this->renderJson($this->output);
	}
	//修改订单
	public function actionEdit(){
		//获取参数
		$request = Yii::app()->request;
		$id = $request->getParam('id');
		//修改
		$form = $request->getParam('dataForm');
		$childForm = $request->getParam('childForm');
		if(!empty($form) && !empty($childForm)){
			$model = new OrderForm;
			$model->attributes = $form;
			if(!$model->validate()){
				$msg = array_values($model->getErrors());
				$this->output['status'] = 10000;
				$this->output['msg'] = empty($msg[0])?Yii::app()->params['any_write']:$msg[0];
				$this->renderJson($this->output);
			}
			$model->editData($id);
			$model = new OrderDetailForm;
			if(!$model->editData($childForm)){
				$msg = array_values($model->getErrors());
				$this->output['status'] = 10000;
				$this->output['msg'] = empty($msg[0])?Yii::app()->params['any_write']:$msg[0];
				$this->renderJson($this->output);
			}
			$this->output['code'] = 200;
			$this->output['navTabId'] = 'order';
			$this->output['callbackType'] = 'closeCurrent';
			$this->renderJson($this->output);
		}
		//获取订单
		$model = new Order;
		$data = $model->getOrder($id);
		$model = new Goods;
		$goodsList = $model->getGoodsValueList();
		$model = new User;
		$user = $model->getUser($data['mainOrder']['user_id']);
		$this->render('edit', array(
				'data'=>$data,
				'goodsList'=>$goodsList,
				'user'=>$user
			));
	}
}